package vn.lumi.smarthome.network;

import java.util.Arrays;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.model.Device;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class ControlNormalDevice extends AppMsg {

	// attributes
	private int NumOfDevice = 0;
	private int[] deviceIDList = null;
	private int[] deviceStateList = null;
	private int[] deviceStateIDList = null;

	public ControlNormalDevice() {
		super(NetMessage.CMD_APP_CONTROL_NORMAL);
	}

	public ControlNormalDevice(Parcel in) {
		readFromParcel(in);
	}

	public ControlNormalDevice(int Msg_id, int uid, int sid,
			int[] DeviceIDList, int[] DeviceStateList, int[] DeviceStateIDList) {
		super(NetMessage.CMD_APP_CONTROL_NORMAL, Msg_id, uid, sid);
		// TODO Auto-generated constructor stub
		NumOfDevice = DeviceIDList.length;
		deviceIDList = new int[NumOfDevice];
		deviceStateList = new int[NumOfDevice];
		deviceStateIDList = new int[NumOfDevice];
		for (int i = 0; i < NumOfDevice; i++) {
			deviceIDList[i] = (DeviceIDList[i]);
			deviceStateList[i] = DeviceStateList[i];
			deviceStateIDList[i] = DeviceStateIDList[i];
		}
	}

	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[5 + 2 * NumOfDevice];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) this.NumOfDevice;
		for (int i = 0; i < NumOfDevice; i++) {
			result[5 + 2 * i] = (byte) deviceIDList[i];
			result[6 + 2 * i] = (byte) deviceStateList[i];
		}
		Log.e("ban tin", "ControlNormalDevice: " + Arrays.toString(result));
		return result;

	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this.Cmd_id);
		dest.writeInt(this.length);
		dest.writeInt(this.Msg_id);
		dest.writeInt(this.NumOfDevice);
		dest.writeInt(this.session_id);
		dest.writeInt(this.user_id);
		dest.writeIntArray(deviceIDList);
		dest.writeIntArray(deviceStateList);
		dest.writeIntArray(this.deviceStateIDList);
	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		for (int i = 0; i < NumOfDevice; i++) {
			// update device state value
			ContentValues values = new ContentValues();
			values.clear();
			// Hieu sua
			// values.put(TBL_DeviceState.ID,
			// String.valueOf(deviceStateIDList[i]));
			//
			values.put(TBL_DeviceState.VALUE, deviceStateList[i]);
			String selection = TBL_DeviceState.ID + "=?";
			String[] selectionArgs = new String[] { String
					.valueOf(deviceStateIDList[i]) + "" };
			context.getContentResolver().update(
					TBL_DeviceState.CONTENT_URI_DEVICE_STATE, values,
					selection, selectionArgs);
			values.clear();
			// clear isControl of device
			
			// Hieu sua
			// values.put(TBL_Device._ID, deviceIDList[i]);
			//
			values.put(TBL_Device.IS_CONTROL, Device.DEVICE_STATE_CONTROL_NO);
			selection = TBL_Device._ID + "=?";
			selectionArgs = new String[] { String.valueOf(deviceIDList[i]) + "" };
			context.getContentResolver().update(TBL_Device.CONTENT_URI_DEVICES,
					values, selection, selectionArgs);

		}
		// Hieu them vao
		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_broadcast_device_control_successful));
		context.sendBroadcast(intent);
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		for (int i = 0; i < NumOfDevice; i++) {
			ContentValues values = new ContentValues();
			values.put(TBL_Device._ID, deviceIDList[i]);
			values.put(TBL_Device.IS_CONTROL, Device.DEVICE_STATE_CONTROL_NO);
			String selection = TBL_Device._ID + "=?";
			String[] selectionArgs = new String[] { deviceIDList[i] + "" };
			context.getContentResolver().update(TBL_Device.CONTENT_URI_DEVICES,
					values, selection, selectionArgs);
		}
		// Hieu them vao
		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_broadcast_device_control_failed));
		context.sendBroadcast(intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		Cmd_id = in.readInt();
		length = in.readInt();
		Msg_id = in.readInt();
		NumOfDevice = in.readInt();
		session_id = in.readInt();
		user_id = in.readInt();
		in.readIntArray(deviceIDList);
		in.readIntArray(deviceStateList);
		in.readIntArray(deviceStateIDList);
	}

	/**
	 * 
	 * This field is needed for Android to be able to create new objects,
	 * individually or as arrays.
	 * 
	 * This also means that you can use use the default constructor to create
	 * the object and use another method to hyrdate it as necessary.
	 * 
	 * I just find it easier to use the constructor. It makes sense for the way
	 * my brain thinks ;-)
	 * 
	 */
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
		public ControlNormalDevice createFromParcel(Parcel in) {
			return new ControlNormalDevice(in);
		}

		public ControlNormalDevice[] newArray(int size) {
			return new ControlNormalDevice[size];
		}
	};

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK ack = new HC_ACK();
		if (ack.isCorrectFormat(input, length)) {
			ack.parseMessage(input, length);
			if (ack.getMsg_id() == this.Msg_id)
				result = true;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if (ack_busy.isCorrectFormatBusy(input, length)) {
			ack_busy.parseMessage(input, length);
			if (ack_busy.getMsg_id() == this.Msg_id)
				result = true;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub

	}

}
